/**************************************************************************
 * @name: GetLcdData.c
 * @brief: 获取LCD的宽和高和色深
 * @date: 2025/01/08
 * @author: boluzs@163.com
 * @note: None
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/fb.h>

// 为了提高程序的可移植性，LCD的设备文件的路径通过宏定义实现
#define LCD_DEV "/dev/fb0"
#define LCD_MAPSIZE 800 * 480 * 4
#define WHITE 0xFFFFFF
#define RED 0xFF0000

int main(int argc, char const *argv[])
{
    // 1.打开LCD，linux系统下把硬件设备抽象为文件，所以可以访问硬件设备的设备文件
    int lcd_fd = open(LCD_DEV, O_RDWR);

    // 错误处理
    if (-1 == lcd_fd)
    {
        fprintf(stderr, "open lcd error,errno = %d,%s\n", errno, strerror(errno));
        exit(-1);
    }

    struct fb_var_screeninfo lcd_info;
    int ioctl_ret = ioctl(lcd_fd, FBIOGET_VSCREENINFO, &lcd_info);
    if (ioctl_ret == -1)
    {
        fprintf(stderr, "ioctl_ret error,errno = %d,%s\n", errno, strerror(errno));
        exit(-1);
    }
    printf("宽：%d,高：%d,色深：%d\n", lcd_info.xres, lcd_info.yres, lcd_info.bits_per_pixel);

    // 4.关闭LCD并解除映射,不会影响已经写入到LCD的设备文件中颜色数据
    close(lcd_fd);
    return 0;
}
